package org.alex.samples.dao;

//import java.text.ParseException;
//import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


//import javax.transaction.HeuristicMixedException;
//import javax.transaction.HeuristicRollbackException;
//import javax.transaction.RollbackException;
//import javax.transaction.SystemException;
//import javax.transaction.Transaction;

import org.alex.samples.model.ExamSubject;
import org.alex.samples.model.ExamTest;
import org.alex.samples.model.UserJDBC;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class UserHibernateDAOImpl extends BaseHibernateDAO<ExamTest> implements IUserHibernateDAO {
	@SuppressWarnings("unused")
	private static final String UserJDBC = null;
	@Autowired
	private SessionFactory sessionFactory;

	public Session getSession() {
		return this.sessionFactory.getCurrentSession();

	}

	public List<ExamTest> findAll() {
		// Session session=getSession();
		// String hql="FROM ExamTest";
		// @SuppressWarnings("unchecked")
		// List<ExamTest> examTestList=session.createQuery(hql).list();
		// return examTestList;
		return this.find("from ExamTest");

	}
	
	

	public ExamTest getExamTestById(Integer Id) {
		// Session session=getSession();
		// return(ExamTest) session.get(ExamTest.class, Id);
		return this.get(Id);
	}

	public void createExamTest(ExamTest examTest) throws Exception {
		// 不使用extends BaseHibernateDAO<ExamTest>的原始方法
		// Session session=getSession();
		// session.save(examTest);

		// 继承BaseHibernateDAO<ExamTest>后的方法
		this.save(examTest);
	}

	public void editExamTest(ExamTest examTest) throws Exception {
		// 不使用extends BaseHibernateDAO<ExamTest>的原始方法
		// Session session=getSession();
		// session.update(examTest);

		// 继承BaseHibernateDAO<ExamTest>后的方法
		// ExamTest exsitsExamTest=this.get(examTest.getId());
		// if(exsitsExamTest!=null){
		// this.update(examTest);
		// }

		this.update(examTest);
	}

	public void deleteExamTest(Integer Id) throws Exception {
		// 不使用extends BaseHibernateDAO<ExamTest>的原始方法
		// Session session=getSession();
		// ExamTest exsitsExamTest=(ExamTest) session.get(ExamTest.class, Id);
		// if(exsitsExamTest!=null){
		// session.delete(exsitsExamTest);
		// }

		// 继承BaseHibernateDAO<ExamTest>后的方法
		ExamTest exsitsExamTest = this.get(Id);
		if (exsitsExamTest != null) {
			this.delete(exsitsExamTest);
		}

	}

	// 查询 考题表和考题类型表的两个表数据
	@SuppressWarnings("unchecked")
	public List<Object[]> findExamTextAndType() {
		Query query = getSession().createQuery("from ExamTest e,ExamTestType t where e.examTestType.id=t.id");
		return query.list();
	}

	
	// 多对多 新增学生并 为学生添加一个科目的方法
	public void test1() {
		// 获取一个会话
		Session session = getSession();
		UserJDBC user=new UserJDBC();
		ExamSubject examSubject=new ExamSubject();
		
		user.setUserId("student25");
		user.setUserName("student25");
		user.setPassword("123");
		user.setGender(1);
		user.setTel("13235398996");
		user.setEmail("8569422@qq.com");
		user.setAddress("深圳");				 
		@SuppressWarnings("deprecation")
		Date date=new Date(2015,2,5);	
		user.setBirthday(date);
		
		examSubject.setSubId(7);		
		examSubject.setName("文综");
		examSubject.setDescription("文综大全");
		examSubject.setTestCount(20);
		examSubject.setTotalScore(60.00);
		examSubject.setTotalTime(60);
		examSubject.setStatus(0);
				
		// 为某学生添加一个科目
		user.getExamSubject().add(examSubject);

		session.save(user);

	}

	// 多对多 显示全部学生与之关联的科目信息方法
	public void test2() {
		Session session = getSession();
		String hql = "select new Map(u.userId as userId,u.userName as userName,u.tel as Tel,e.subId as subjectId,e.name as subName,e.description as Description) from UserJDBC u join u.examSubject e ";
		@SuppressWarnings("unchecked")
		List<Map<String, Object>> list = session.createQuery(hql).list();
		for (Map<String, Object> data : list)
			System.out.println(data);

	}
	
	
	//多对多  为某学生删除一个科目
	public void test3(){
		Session session =getSession();
		//获取要删除科目的学生对象
		UserJDBC user = (UserJDBC) session.get(UserJDBC.class, "1");
		//获取操作的科目对象
		ExamSubject examSubject=(ExamSubject) session.get(ExamSubject.class, 3);
		//将科目从学生的科目集合中移除
		user.getExamSubject().remove(examSubject);
				
	}
	
	
	
	//级联删除学生
	public void test4(){
		Session session =getSession();
		UserJDBC user = (UserJDBC) session.get(UserJDBC.class, "2");		
		session.delete(user);
		
	}

	
	//级联删除科目
		public void test5(){
			Session session =getSession();			
			ExamSubject examSubject=(ExamSubject) session.get(ExamSubject.class, 5);
			session.delete(examSubject);
		}
	
}
